<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/include/base.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<link rel="shortcut icon" href="img/favicon.png"/>
<script type="text/javascript" src="static/js/dept.js"></script>
<script  type="text/javascript"	src="static/js/pageComm.js"></script>


<script type="text/javascript">
var currentPage;
		$(function(){
			
			$(document).on("click",".updateDType",function(){
				//清空表单样式及内容
				reset_form("#dictionaryForm");
				//设置模态框标题
				$("#dictionaryLabel").text("字典类型信息修改");
				//设置输入框标题
				 $(".control-label").eq(0).html("字典类型名称");
				 $(".control-label").eq(1).html("所属模块");
					
				//设置name属性
				 $(".form-control").eq(0).attr("name","dTypeName");
				 $(".form-control").eq(1).attr("name","moduleId");
				 $(".form-control").eq(2).attr("name","dTypeRemark");

				
				//加载所有的模块信息  放入多选框中
				 getAllModule("${APP_PATH}/module","#dictorySelect select");

				//根据id获取 该字典类型的详细信息 
				getDTypeById($(this).attr("dTypeId"));
				//打开模态框
				$("#dictionaryModal").modal({
					backdrop: "static"
				});
			});
			
			
			$("#newDType").click(function(){
				//清空表单样式及内容
				reset_form("#dictionaryForm");
				//设置模态框标题
				$("#dictionaryLabel").text("新增字典类型");
				//设置输入框标题
				 $(".control-label").eq(0).html("字典类型名称");
				 $(".control-label").eq(1).html("所属模块");
				 
				//设置name属性
				 $(".form-control").eq(0).attr("name","dTypeName");
				 $(".form-control").eq(1).attr("name","moduleId");
				 $(".form-control").eq(2).attr("name","dTypeRemark");
				
				//加载所有的模块信息  放入多选框中
				 getAllModule("${APP_PATH}/module","#dictorySelect select");

				//打开模态框
				$("#dictionaryModal").modal({
					backdrop: "static"
				});
			});
			
			//新增获取 更新部门
			$("#saveDictionary").click(function(){
				//alert($("#dictionaryForm").serialize());
				//alert($("#dTypeIdHidden").val())
				if(undefined == $("#dTypeIdHidden").val() || $("#dTypeIdHidden").val()=='' || $("#dTypeIdHidden").val() ==null){
				//	alert("新建");
					//新建字典
					newDictionaryOther("${APP_PATH}/dType","#dictionaryForm","#dictionaryModal");
					
				}else{
				//	alert("更新");
					//更新字典
					updateDictionOther("${APP_PATH}/dType","#dictionaryForm","#dictionaryModal");
				}
			});
			
			//删除一条记录
			$(document).on("click",".deleteDType",function(){
				var dTypeId=$(this).attr("dTypeId");
 				var dTypeName=$(this).attr("dTypeName");
 				
 				if(confirm("确认删除 【 "+ dTypeName+" 】 ？")){
 					$.ajax({
 						url:"${APP_PATH}/dType/"+dTypeId,
 						type:"DELETE",
 						success:function(result){
 							to_page(currentPage);
 						}
 					});
 				}
			})
			
 			//删除多条消息
 			$("#deleteDTypeList").click(function(){
 				
 				var checked=$("#dTypeTable tbody input:checked");

 				 var dTypeIds="";
 				 var dTypeNames="";
 				 checked.each(function(index,value){
 					dTypeNames += $(this).attr("dTypeName")+",";
 					dTypeIds += $(this).attr("dTypeId")+"-";
 				 });
 				 dTypeNames=dTypeNames.substring(0,dTypeNames.length-1);
 				dTypeIds=dTypeIds.substring(0,dTypeIds.length-1);

 				 if(confirm("确认要删除 【 "+dTypeNames+" 】 吗？")){
 					 $.ajax({
 						 url:"${APP_PATH}/dType/"+dTypeIds,
 						 type:"DELETE",
 						 success:function(result){
 							 //返回当前页
 							 to_page(currentPage);
 						 }
 					 });
 				 }
 			});

			
			//显示第一页的数据
			to_page(1);

		});
		
		function getDTypeById(dTypeId){
			$.ajax({
				url:"${APP_PATH}/dType/"+dTypeId,
				type:"GET",
				success:function(result){
					showDTypeFromModal(result.extend.dType);
				}
			});
		}
		
		function showDTypeFromModal(dType){
			$("#dictionaryForm").append($("<input type='hidden' name='id' id='dTypeIdHidden'></input>").val(dType.id));
			$("#dName").val(dType.dTypeName);
			$("#dictorySelect select").val(dType.moduleId);
			$("#remark").val(dType.dTypeRemark);
		}
		
		function to_page(pn){

			$.ajax({
				url:"${APP_PATH}/dTypes/"+pn,
				type:"GET",
				success:function(result){
					//2. 解析并显示分页数据
	 				  build_page_info(result);
	 				 //3解析显示分页条
	 				  build_page_nav(result);
	 				 
	      				//1. 解析并显示员工数据
	 				 build_emps_table(result);
				}
			});
		}

		//1. 解析并显示员工数据
			function build_emps_table(result){
				//清空table表格
		  		$("#dTypeTable tbody").empty();
				//获取返回的dept值
				var dType=result.extend.pageInfo.list;

                  $.each(dType,function(index,item){
						var checkboxTd=$("<td></td>").append($("<input></input>")
								.addClass("checkbox")
								.attr("dTypeName",item.dTypeName)
								.attr("dTypeId",item.id)
								.attr( "type","checkbox")).attr("align","center");
						
						var idTd=$("<td></td>").append(item.id);
						var dTypeNameTd=$("<td></td>").append(item.dTypeName);
						
						if(item.module ==null || item.module == ""){
							var moduleNameTd=$("<td></td>");
						}else{
							var moduleNameTd=$("<td></td>").append(item.module.moduleName)
						}
						
						var editBtn=$("<a></a>").addClass("btn btn-primary updateDType").append("修改");
						var deleteBtn=$("<a class='btn btn-danger deleteDType' >删除</a>")
						editBtn.attr("dTypeId",item.id);
						deleteBtn.attr("dTypeId",item.id);
						deleteBtn.attr("dTypeName",item.dTypeName);
						var btnGroupTd=$("<td style='text-align:center;'></td>").append($("<div></div>").addClass("btn-group"));
						
						btnGroupTd.append(editBtn).append(deleteBtn);
						
						$("<tr></tr>").append(checkboxTd)
									  .append(idTd)
									  .append(dTypeNameTd)
									  .append(moduleNameTd)
									  .append(btnGroupTd)
									  .appendTo($("#dTypeTable tbody"));
						
					});
		  	}
		
		</script>
</head>


  <body>
  <!-- 模态框 -->
  <%@include file="/WEB-INF/include/dictionaryModal.jsp" %>
  
  <!-- container section start -->
  <section id="container" class="">
      <%@include file="/WEB-INF/include/top.jsp"%>

      <%@include file="/WEB-INF/include/left.jsp"%>

      <!--main content start-->
      <section id="main-content" >
          <section class="wrapper">
		  <div class="row">
				<div class="col-lg-12">
					<ol class="breadcrumb">
						<li><i class="fa fa-home"></i><a href="index.jsp">字典管理</a></li>
						<li><i class="fa fa-table"></i>字典类型</li>
					</ol>
					
				</div>
			</div>
	          
             
            
            
              <div class="row" align="center" style="margin:0 auto">
                  <div class="col-lg-12">
                      <section class="panel">
                        
                          <table class="table table-striped table-advance table-hover" id="dTypeTable">
                          <thead>
                          	  <tr>
                                 <th width="8%" style="text-align:center;"><input type="checkbox" id="checkAll" /></th>
                                 <th width="13%">ID</th>
                                 <th width="37%">字典类型名称</th>
                                 <th width="20%">所属模块</th>
                                 <th width="22%" style="text-align: center;"><span>操作&nbsp;&nbsp;&nbsp;</span>
                                 	<span class="btn-group" >
                                      <a class="btn btn-primary" id="newDType" >新增</a>
                                      <a class="btn btn-danger" id="deleteDTypeList">删除</a>
                      				</span>
                                 </th>
                              </tr>
                          </thead>
                           <tbody></tbody>
                           <tfoot>
                           <tr><td colspan="5">
                           
                    <div class="row " >
			<!--分页文字信息  -->
			<div class="col-md-6" id="page_info_area"></div>
			<!-- 分页条信息 -->
			<div class="col-md-6" id="page_nav_area" ></div>
		</div>       
                           
                           </td></tr>
        
                           </tfoot>
                        </table>
                      </section>
                  </div>
              </div>
     
          </section>
      </section>
      <!--main content end-->
  </section>
  <!-- container section end -->
    <!-- javascripts -->
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- nicescroll -->
    <script src="js/jquery.scrollTo.min.js"></script>
    <script src="js/jquery.nicescroll.js" type="text/javascript"></script>
    <!--custome script for all page-->
    <script src="js/scripts.js"></script>


  </body>
</html>
